#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <map>

using namespace std;
//https://leetcode.cn/problems/random-pick-index/submissions/559538403/
//https://leetcode.cn/problems/longest-substring-without-repeating-characters/submissions/557458406/
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int res=0;
        if(s.empty() || s.size()==1) return s.size();
        //"pwwkew";"abcabcbb";"bbbbb"
        int left=0;
        int right=1;
        std::map<char,int> map1;
        map1[s[0]]=0;
        int max_len=0;
        while(left<=right&& right<s.size()){
            if(map1.find(s[right]) != map1.end()){
                int same_index=map1[s[right]];
                while(left<=same_index){
                    map1.erase(s[left]);
                    ++left;
                }
                max_len=std::max(max_len,right-left+1);
                map1[s[right]]=right;
                ++right;
            }else{
                max_len=std::max(max_len,right-left+1);
                map1[s[right]]=right;
                ++right;
            }
        }
        return max_len;
    }
};